<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" :height="autoHeight" @row-click="handleRowClick" @row-contextmenu="rightClick">
      <el-table-column prop="callCmd" label="指令" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="dbSchema" label="库名" align="center" show-overflow-tooltip>
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dbMain">{{scope.row[scope.column.property]}}</el-tag>
          <span v-else>{{scope.row[scope.column.property]}}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="select" label="操作" align="center" width="170px">
        <div v-if="!select" slot="header">
          <el-button type="primary" @click="visible = true" icon="el-icon-circle-plus-outline"></el-button>
        </div>
        <div slot-scope="scope">
          <template v-if="select">
            <el-button type="success" icon="el-icon-thumb" circle @click="$emit('rowSelect',scope.row)" title="选择"></el-button>
          </template>
          <template v-else>
            <el-button type="success" icon="el-icon-edit" circle @click="() => { entityInfo=$Util.copy(scope.row);visible=true}" title="编辑"></el-button>
            <el-button type="warning" icon="el-icon-document-copy" circle @click="copy(scope.row)" title="复制"></el-button>
            <el-button type="info" icon="el-icon-refresh" circle @click="() => { entityInfo=$Util.copy(scope.row);initVisible=true}" title="初始化"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
          </template>
        </div>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>

    <el-dialog :visible.sync="visible" title="数据库连接信息">
      <el-form label-position="right" label-width="80px">
        <el-row :gutter="8">
          <el-col :span="12">
            <el-form-item label="名称" required>
              <el-input v-model="entityInfo.dbName" placeholder="数据库名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指令" required>
              <el-input v-model="entityInfo.callCmd" placeholder="数据库指令"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="主机" required>
              <el-input v-model="entityInfo.dbHost" placeholder="连接主机"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="端口" required>
              <el-input-number v-model="entityInfo.dbPort" controls-position="right" placeholder="连接端口" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="库名" required>
              <el-input v-model="entityInfo.dbSchema" placeholder="数据库名"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户" required>
              <el-input v-model="entityInfo.dbUser" placeholder="连接用户"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="密码" required>
              <el-input v-model="entityInfo.dbPass" placeholder="连接密码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="主数据库">
              <el-switch v-model="entityInfo.dbMain"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input type="textarea" :rows="4"  v-model="entityInfo.dbNote" placeholder="数据库功能描述"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button icon="el-icon-circle-check" type="success" @click="saveData(entityInfo)">{{ entityInfo.roleId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";
import TableTable from "@/views/system/db/TableTable";

export default {
  name: "DbTable",
  components: {TableTable},
  mixins: [IdeaTable],
  props: {
    select: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      initVisible: false,
      scriptTag: [
          { value: 'store', label: '文件存储' },
          { value: 'process', label: '业务流程' },
      ]
    }
  },
  computed:{
    autoHeight() {
      return this.select ? `300px` : `calc(100vh - 130px)`
    }
  },
  watch: {
    initVisible() {
      if (!this.initVisible) {
        this.initEntity()
      }
    }
  },
  created() {
    this.queryUrl = '/db/queryPage'
    this.saveUrl = '/db/save'
    this.removeUrl = '/db/delete/'
    this.rowKey = 'dbId'
    this.rightButtons.splice(0, this.rightButtons.length)
    this.appendRightBtn({ label: '新增', icon: 'el-icon-circle-plus-outline', click: this.add })
    this.appendRightBtn({ label: '编辑', icon: 'el-icon-edit', click: this.editDb })
    this.appendRightBtn({ label: '复制', icon: 'el-icon-document-copy', click: this.copy })
    this.appendRightBtn({ label: '删除', icon: 'el-icon-delete', click: this.removeData })
    this.loadSource()
  },
  methods: {
    add() {
      this.visible = true
    },
    copy(row) {
      this.entityInfo = this.$Util.copy(row)
      this.$delete(this.entityInfo, this.rowKey)
      this.$delete(this.entityInfo, 'callCmd')
      this.$delete(this.entityInfo, 'dbSchema')
      this.$set(this.entityInfo, 'dbMain',false)
      this.visible = true
    },
    editDb(row) {
      this.entityInfo = this.$Util.copy(row)
      this.visible = true
    }
  }
}
</script>

<style scoped>

</style>
